// pages/home/activityDetail/index.js
import {
  home
} from '../../../api/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    activeTab: 0,
    obj: {
      '0': 'DetailPosterUrl',
      '1': 'IntroducePosterUrl',
      '5': 'AwardPosterUrl'
    },
    data: {},
    loadImgNum: 0,
    aaNnmImag: 1
  },
  onImgLoad() {
    if (this.data.aaNnmImag == this.data.loadImgNum) {
      for (const key in this.data.obj) {
        wx.createSelectorQuery().select(`#${this.data.obj[key]}`).boundingClientRect((rect) => {
          if (rect && rect.top) {
            this.setData({
              [this.data.obj[key]]: rect.top - 36
            })
          }
        }).exec()
      }
      wx.hideLoading()
    }
    this.setData({
      aaNnmImag: this.data.aaNnmImag + 1
    })
  },
  toScroll(num) {
    wx.pageScrollTo({
      scrollTop: this.data[this.data.obj[num]],
      duration: 300
    });
  },

  clickTab(e) {
    let index = e.target.dataset.index
    this.toScroll(index)
    if (index !== this.data.activeTab) {
      this.setData({
        activeTab: index
      })
    }
  },
  goPage() {
    wx.switchTab({
      url: '/pages/ai/index',
    })
  },
  activitToPage(e) {
    let id = e.target.dataset.id
    wx.navigateTo({
      url: `/pages/home/activityDetail/activityContent/index?id=${id}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading()
    console.log(options.id);
    this.setData({
      id: options.id
    })
    wx.setNavigationBarTitle({
      title: options.name
    })
    home.getthemedetail({
      "ID": options.id,
      "ActivePort": 3
    }).then(res => {
      if (res.StatusCode == 200) {
        let num = 0
        res.Result.TabButtonList.forEach(element => {
          if (res.Result[this.data.obj[element.ButtonType]]) num++
        });
        this.setData({
          data: res.Result,
          loadImgNum: num
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          icon:'none',
          title: res.Message,
        })
      }


    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})